﻿#region License
/* Copyright (C) 2009 Tim Coulter
 * 
 * This file is part of ExtremeML.
 * 
 * ExtremeML is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public
 * License, as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 * the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
*/
#endregion

using System;
using DocumentFormat.OpenXml.Packaging;
using ExtremeML.Spreadsheet;

namespace ExtremeML.Packaging
{
	// This is a partial class that adds custom functionality to its auto-generated partner.
	public sealed partial class WorksheetPartWrapperCollection
	{
		#region Custom Properties

		/// <summary>
		/// Gets the containing package as a <see cref="SpreadsheetDocumentWrapper"/>.
		/// </summary>
		public SpreadsheetDocumentWrapper Spreadsheet
		{
			get { return (SpreadsheetDocumentWrapper)Package; }
		}

		/// <summary>
		/// Gets a <see cref="WorksheetPartWrapper"/> by the name of its wrapped <see cref="WorksheetPart"/>.
		/// </summary>
		public WorksheetPartWrapper this[string name]
		{
			get { return Items.Find(item => item.Name == name); }
		}

		#endregion
		#region Custom Methods

		/// <summary>
		/// Returns the wrapper for the worksheet part having the specified sheet id.
		/// </summary>
		/// <param name="sheetId">The sheet id associated with the target worksheet part.</param>
		/// <returns>A <see cref="WorksheetPartWrapper"/> that matches the specified id. <c>null</c> if no match is found.</returns>
		public WorksheetPartWrapper Find(
			int sheetId)
		{
			return Items.Find(item => item.Sheet.SheetId.Value == sheetId);
		}

		/// <summary>
		/// Returns a <see cref="WorksheetPartWrapper"/> that wraps a new worksheet with the specified unique name.
		/// </summary>
		/// <param name="name">The unique name of the new worksheet.</param>
		/// <returns>A new instance of <see cref="WorksheetPartWrapper"/>.</returns>
		public WorksheetPartWrapper Add(
			string name)
		{
			SheetWrapper.ValidateUniqueName(Spreadsheet, name);
			var result = Add();
			result.Name = name;
			result.Activate();
			return result;
		}

		/// <summary>
		/// Deletes the <see cref="WorksheetPartWrapper"/> that wraps the worksheet having the specified name.
		/// </summary>
		/// <param name="name">The name of the target worksheet.</param>
		public void Delete(
			string name)
		{
			Remove(this[name]);
		}

		#endregion
		#region Overrides

		/// <summary>
		/// Performs housekeeping and validation of a collection member addition.
		/// </summary>
		protected override void OnCustomAdding(
			WorksheetPartWrapper part)
		{
			Spreadsheet.WorkbookPart.Workbook.Sheets.Sheets.Add(SpreadsheetDocumentWrapper.GetUniqueIdString(), part.Id);
		}

		/// <summary>
		/// Performs housekeeping and validation of a requested deletion.
		/// </summary>
		protected override bool OnCustomRemoving(
			WorksheetPartWrapper part)
		{
			// Enforce the schema-defined minimum worksheet count.
			if (Count == 1)
			{
				throw new InvalidOperationException("A workbook must contain at least one worksheet");
			}

			Spreadsheet.WorkbookPart.Workbook.Sheets.Sheets.Delete(part.Id);
			return true;
		}

		#endregion
		#region Implementation Methods


		#endregion
		#region Static Methods


		#endregion
	}
}
